'''import unittest
from alg import Solution, TreeNode
from gen import generate_test_cases, tree_to_list

def is_bst(root: TreeNode) -> bool:
    """验证是否为二叉搜索树"""
    stack = []
    prev_val = float('-inf')
    current = root
    
    while current or stack:
        while current:
            stack.append(current)
            current = current.left
        
        current = stack.pop()
        if current.val <= prev_val:
            return False
        prev_val = current.val
        current = current.right
    
    return True

def get_height(root: TreeNode) -> int:
    """获取树的高度"""
    if not root:
        return 0
    left_height = get_height(root.left)
    right_height = get_height(root.right)
    return max(left_height, right_height) + 1

def is_balanced(root: TreeNode) -> bool:
    """验证树是否平衡"""
    if not root:
        return True
    
    left_height = get_height(root.left)
    right_height = get_height(root.right)
    
    if abs(left_height - right_height) > 1:
        return False
    
    return is_balanced(root.left) and is_balanced(root.right)

class TestSortedArrayToBST(unittest.TestCase):
    def setUp(self):
        self.solution = Solution()
    
    def test_empty_array(self):
        root = self.solution.sortedArrayToBST([])
        self.assertEqual(root, None)
    
    def test_single_element(self):
        root = self.solution.sortedArrayToBST([5])
        self.assertEqual(root.val, 5)
        self.assertEqual(root.left, None)
        self.assertEqual(root.right, None)
    
    def test_two_elements(self):
        root = self.solution.sortedArrayToBST([1, 2])
        self.assertEqual(root.val, 1)
        self.assertEqual(root.left, None)
        self.assertEqual(root.right.val, 2)
    
    def test_three_elements(self):
        root = self.solution.sortedArrayToBST([1, 2, 3])
        self.assertEqual(root.val, 2)
        self.assertEqual(root.left.val, 1)
        self.assertEqual(root.right.val, 3)
    
    def test_example_case(self):
        root = self.solution.sortedArrayToBST([-10, -3, 0, 5, 9])
        self.assertEqual(tree_to_list(root), [0, -10, 5, None, -3, None, 9])
    
    def test_random_cases(self):
        test_cases = generate_test_cases(20)
        for nums in test_cases:
            root = self.solution.sortedArrayToBST(nums)
            
            # 验证是否为二叉搜索树
            self.assertTrue(is_bst(root))
            
            # 验证是否平衡
            self.assertTrue(is_balanced(root))
            
            # 验证中序遍历结果是否与原数组一致
            inorder = []
            def inorder_traversal(node):
                if node:
                    inorder_traversal(node.left)
                    inorder.append(node.val)
                    inorder_traversal(node.right)
            inorder_traversal(root)
            self.assertEqual(inorder, nums)

if __name__ == "__main__":
    unittest.main()
'''
from alg import Solution, TreeNode
from gen import generate_sorted_array, tree_to_list

if __name__ == "__main__":
    # 生成随机有序数组
    sorted_array = generate_sorted_array()
    print("随机生成的有序数组:", sorted_array)

    # 创建解决方案实例
    solution = Solution()
    # 将有序数组转换为二叉搜索树
    root = solution.sortedArrayToBST(sorted_array)
    # 将二叉树转换为层序遍历列表
    bst_list = tree_to_list(root)
    # 将列表中的 None 替换为 'null'
    formatted_list = ['null' if val is None else str(val) for val in bst_list]
    # 将列表元素用逗号连接成字符串
    output_str = ','.join(formatted_list)
    print("由有序数组组成的搜索二叉树的层序遍历结果:", output_str)